package org.opens.util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.system.OsInfo;
import org.opens.exception.BusinessException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileUtil {

    private static Log log = LogFactory.get(FileUtil.class);

    public static void openFolder(String folderPath) {
        File file = new File(folderPath);
        if (!file.exists()) {
            return;
        }
        Runtime runtime = null;
        try {
            runtime = Runtime.getRuntime();

            OsInfo osInfo = new OsInfo();

            // 如果是windows, 则打开窗口
            if (osInfo.isWindows()) {
                runtime.exec("cmd /c start explorer " + folderPath);
            }
        } catch (Exception e) {
            //
        } finally {
            // 释放资源
            if (null != runtime) {
                runtime.runFinalization();
            }
        }
    }

    public static void createFolder(String parentPath) {
        File file = new File(parentPath);

        // 解决已经存在的目录再次创见会报错
        if (file.exists()) {
            return;
        }

        if (file.mkdirs()) {
            log.info("多级层文件夹创建成功: " + file.getPath());
        } else {
            throw new BusinessException("多级层文件夹创建失败: " + parentPath);
        }
    }

    /**
     * 将多个路径组装成合法的路径
     * @param path 多个路径信息
     */
    public static String pathSplicing(String... path) {
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < path.length; i++) {
            String pathTmp = removePathSuffix(path[i]);

            result.append(pathTmp);

            if (!pathTmp.contains(".")) {
                result.append(File.separator);
            }
        }

        return result.toString();
    }

    private static String removePathSuffix(String path) {
        for (int i = 0; i < 2; i++) {
            path = StrUtil.removeSuffix(path, "\\");
            path = StrUtil.removeSuffix(path, "\\");

            path = StrUtil.removeSuffix(path, "/");
            path = StrUtil.removeSuffix(path, "/");

            path = StrUtil.removePrefix(path, "\\");
            path = StrUtil.removePrefix(path, "\\");

            path = StrUtil.removePrefix(path, "/");
            path = StrUtil.removePrefix(path, "/");
        }
        return path;
    }

    /**
     * 读取文件内容为String
     * @param path 文件路径
     */
    public static String readFileAsString(String path) {
        StringBuilder sb = new StringBuilder();

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            byte[] cache = new byte[1024];

            int len = -1;
            while ((len = fileInputStream.read(cache)) != -1) {
                sb.append(new String(cache, 0, len));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IoUtil.close(fileInputStream);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        String java = pathSplicing("E:\\gen-code\\", "\\src\\main\\", "\\java\\", "\\Entity.java");
        System.out.println(java);

        String path = "C:\\Users\\zhangyuming\\Desktop\\jarTemp\\a\\001\\a.txt";
        String s = readFileAsString(path);
        System.out.println(s);
    }


}
